package com.itxiuyixiu.homework.admin.controller;

import com.itxiuyixiu.homework.admin.service.AdminRewardsAndPublishService;
import com.itxiuyixiu.homework.entity.dto.RewardsAndPunishmentDto;
import com.itxiuyixiu.homework.entity.vo.RewardsAndPunishmentVo;
import com.itxiuyixiu.homework.utils.ReturnUtils.ReturnMessage;
import com.itxiuyixiu.homework.utils.ReturnUtils.ReturnVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

/**
 * @author 黄磊
 * @date 2020/3/9
 */
@Api(tags = "管理员-奖惩系统")
@RestController
public class AdminRewardsAndPublishController {
    private final AdminRewardsAndPublishService adminRewardsAndPublishService;

    public AdminRewardsAndPublishController(AdminRewardsAndPublishService adminRewardsAndPublishService) {
        this.adminRewardsAndPublishService = adminRewardsAndPublishService;
    }

    @ApiOperation(value = "查询奖惩情况", response = RewardsAndPunishmentVo.class, notes = "只有id、studentId、rewarded有效，其他无效")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "课程情况id",type = "Integer" , dataType = "int"),
            @ApiImplicitParam(name = "studentId", value = "学号"),
            @ApiImplicitParam(name = "rewarded", value = "奖励还是惩罚", dataType = "boolean"),
    })
    @GetMapping("/admin/rewards_publish/message")
    public ReturnVo getAllRewardsAndPunishmentMessage(RewardsAndPunishmentDto dto) {
        return ReturnMessage.getSuccessReturn(adminRewardsAndPublishService.getRewardsAndPunishmentMessage(dto));
    }

    @ApiOperation(value = "添加奖惩记录", response = String.class)
    @PostMapping("/admin/rewards_publish/message")
    public ReturnVo addRecord(RewardsAndPunishmentDto dto) {
        switch (adminRewardsAndPublishService.addRecord(dto)) {
            case 0:
                return ReturnMessage.getSuccessReturn("添加记录成功");
            case 1:
                return ReturnMessage.getFailureReturn("学生不存在");
            default:
                return ReturnMessage.getFailureReturn("服务器出现未知错误");
        }
    }

    @ApiOperation(value = "修改奖惩记录", response = String.class)
    @PutMapping("/admin/rewards_publish/message")
    public ReturnVo modifyRecord(int recordId, RewardsAndPunishmentDto dto) {
        switch (adminRewardsAndPublishService.modifyRecord(recordId, dto)) {
            case 0:
                return ReturnMessage.getSuccessReturn("修改记录成功");
            case 1:
                return ReturnMessage.getFailureReturn("记录不存在");
            case 2:
                return ReturnMessage.getFailureReturn("学生不存在");
            default:
                return ReturnMessage.getFailureReturn("服务器出现未知错误");
        }
    }

    @ApiOperation(value = "删除奖惩记录", response = String.class)
    @DeleteMapping("/admin/rewards_publish/message")
    public ReturnVo deleteRecord(int recordId) {
        if (!adminRewardsAndPublishService.deleteRecord(recordId)) {
            return ReturnMessage.getFailureReturn("记录不存在");
        }
        return ReturnMessage.getSuccessReturn("成功删除");

    }
}
